import router from '@/router'

import store from '@/store'

router.beforeEach(async(to, from, next) => {
    // to and from are both route objects. must call `next`.
    console.log(store.state.user.token)
    const White = ['/404', '/login']
    if (store.getters.token) {
        if (to.path.toLowerCase() === '/login') {
            next('/')
        } else {
            // 验证token，获取个人用户信息
            if (!store.state.user.userInfo.userId) {
                await store.dispatch('user/getUserInfo')
            }
            next()
        }
    } else {
        if (White.includes(to.path.toLowerCase())) {
            next()
        } else {
            next('/login?topath=' + to.fullPath)
        }
    }
})